layui.use(["element"], function () {
    let element = layui.element;
    let hideBtn = $('#hideBtn');
    let mainLayout = $('#main-layout');
    let mainMask = $('.main-mask');
    let customCommon = {

        _init: function () {
            let _this = this;
            _this._initAjax();
            $(document).ready(function () {
                _this._initPage();
                _this._initSidebar();
            });
        },


        _initAjax: function () {
            $.ajaxSetup({
                contentType: "application/x-www-form-urlencoded;charset=utf-8",
                complete: function (XMLHttpRequest, textStatus) {
                    if (XMLHttpRequest && XMLHttpRequest.responseJSON) {
                        let response = XMLHttpRequest.responseJSON;
                        if (response.code && response.code == "3") {
                            layer.msg("");
                        }
                    }
                },
            });
        },

        _initPage: function () {
            var navA = $(element).find('a');
            var id = navA.attr('data-id');
            var isActive = $('.main-layout-tab .layui-tab-title').find("li[lay-id=" + id + "]");
            if (isActive.length > 0) {
                //切换到选项卡
                element.tabChange('tab', id);
            }
            mainLayout.removeClass('hide-side');

            hideBtn.on('click', function () {
                if (!mainLayout.hasClass('hide-side')) {
                    mainLayout.addClass('hide-side');
                } else {
                    mainLayout.removeClass('hide-side');
                }
            });
            element.render();
            element.on('nav(leftNav)', function (elem) {
                let navA = $(elem).find('a');
                let id = navA.attr('data-id');
                mainLayout.removeClass('hide-side');
            });
            //监听导航点击
            element.on('nav(rightNav)', function (elem) {
                let navA = $(elem).find('a');
                let id = navA.attr('data-id');
                console.table(navA);
            });

        },

        _initSidebar:function () {
            let pageIndex = $("#page-index").val();
            let aChildList = $("#sidear-ul").find("a");
            $("#sidear-ul").find("li").removeClass("layui-nav-itemed");
            for (let i = 0; i < aChildList.length; i++) {
                let item = aChildList[i];
                if($(item).data("id") == pageIndex){
                    $(item).parent().addClass("layui-this");
                    $(item).parent().parent().parent().addClass("layui-this");
                }
            }
        }

    };
    customCommon._init();
});